/* Copyright (c) 2008 Google Inc.
*  Copyright (c) 2009 Pierre Henri Kuate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
public static class BatchRequests
{
    public static void Run()
    {
        var c = new org.opensocial.client.OpenSocialClient("orkut.com");

        // orkut supports both the REST and RPC protocols; RPC is preferred because
        // RPC supports batch requests
        c.setProperty(org.opensocial.client.OpenSocialClient.Properties.RPC_ENDPOINT,
                      "http://sandbox.orkut.com/social/rpc");

        // Credentials provided here are associated with the gadget located at
        // http://opensocial-resources.googlecode.com/svn/samples/rest_rpc/sample.xml;
        // If you install this gadget, you can substitute your own OpenSocial ID
        // for the one used below and fetch your profile data and friends
        c.setProperty(org.opensocial.client.OpenSocialClient.Properties.CONSUMER_SECRET, "uynAeXiWTisflWX99KU1D2q5");
        c.setProperty(org.opensocial.client.OpenSocialClient.Properties.CONSUMER_KEY, "orkut.com:623061448914");
        c.setProperty(org.opensocial.client.OpenSocialClient.Properties.VIEWER_ID, "03067092798963641994");

        // OpenSocialBatch objects are used to bulk multiple OpenSocial data
        // requests so they can be submitted to the container (orkut in this
        // case) in a single HTTP request
        var batch = new org.opensocial.client.OpenSocialBatch();

        // Add two data requests to the batch object, one for the profile details
        // and the other for the friends of the specified user; each request is
        // identified by a unique String ID so the results can be accessed in
        // the response returned
        batch.addRequest(org.opensocial.client.OpenSocialClient.newFetchPersonRequest("03067092798963641994"),
                         "person");
        batch.addRequest(org.opensocial.client.OpenSocialClient.newFetchFriendsRequest("03067092798963641994"),
                         "friends");

        // Submit the batch request -- the JSON returned by the container is
        // parsed into an OpenSocialResponse object
        org.opensocial.client.OpenSocialResponse resp = batch.send(c);

        // Retrieve the individual components of the submitted batch -- an
        // Person object representing the requested person and a
        // Collection of these objects representing the requested friends;
        // provide the request ID that you specified when you added the
        // request to the batch
        org.opensocial.data.Person person = resp.getItemAsPerson("person");
        var friends = resp.getItemAsPersonCollection("friends", c.UseRpc);

        System.Console.Out.WriteLine("----------");

        // Output the name and ID of the requested person;
        System.Console.Out.WriteLine("Info. for " + person.DisplayName);
        System.Console.Out.WriteLine("ID: " + person.Id);

        // Retrieve individual fields using the getField method; fields may be
        // complex (objects or arrays) or simple (Strings), which you can
        // determine by querying the object using the isComplex method.
        // The thumbnail URL should be a simple field, so we'll output
        // the value as a String
        org.opensocial.data.OpenSocialField thumbnailUrlField = person.getField("thumbnailUrl");
        if (!thumbnailUrlField.IsComplex)
        {
            System.Console.Out.WriteLine("Thumbnail URL: " + thumbnailUrlField.StringValue);
        }

        System.Console.Out.WriteLine("Friends:");

        // Iterate through the returned Collection
        foreach (org.opensocial.data.Person friend in friends)
        {
            // Output the name of the current person
            System.Console.Out.WriteLine("- " + friend.DisplayName);
        }

        System.Console.Out.WriteLine("----------");
    }
}